<?php

namespace MLM\Model\Network;


use MLM\Model\Network;

class Storage extends \MLM\Model\Generic\Db\Storage {

    
    public function getMemberDetail($key){
        
        $sql = "select x.*, sp.member_code as sponsor_code, sp.member_name as sponsor_name from (
                select m.member_id, m.member_code, m.member_name, m.pin, a.contact_name, a.contact_email, a.contact_mobile,
                a.contact_phone, a.contact_address, a.contact_city, a.contact_province, a.country_id, a.post_code,
                account_name, bank_acc_no, bank_city_name, bank_acc_name, member_type_name,
                m.sponsor_id,
                m.matrik1_id, parent_id, status_id, m.member_type_id, m.subs_start_dat,
                m.subs_end_dat, m.subs_amount, m.total_subs_amount 
                from members m, member_contact c, contact a, accounts ac, member_type mt
                where m.member_id = ?
                and c.member_id = m.member_id 
                and a.contact_id = c.contact_id
                and ac.member_id = m.member_id and mt.member_type_id = m.member_type_id
                ) x left outer join members sp on sp.member_id = x.sponsor_id";

        //$sql = "select * from members where member_id = ?";

        $stmt = $this->_db->query($sql);
        $res = $stmt->execute(array($key));
        

        //$res = $this->_db->fetchRow($sql);
/*
        $sqlW = "select a.contact_name as waris_name,
                w.email as waris_email,
                w.mobile_no as waris_mobile,
                w.phone as waris_phone,
                w.no_ktp as waris_ktp,
                w.member_contact_id as member_contact_idw
                from member_contact w, contact c
                where w.member_id = ? and w.contact_type_id = 2";

        $stmt2 = $this->_db->query($sqlW);
        $resW = $stmt2->execute(array($memberId));
 * 
 */

        //$resW = $this->_db->fetchRow($sqlW);

/*
        if (is_array($resW)){
            return array_merge($res, $resW);
        }else
 * 
 */
        return $res;
    }



    function getSponsorNetwork($sBy, $key, $parentCode) {
        
            $sql = "select m.*, ca.contact_mobile, concat(ca.contact_address,' ',ca.contact_city) as addr
                    from members m, member_contact mc, contact ca
                    where m.$sBy  = ? and (member_type_id = 2 or member_type_id > 4) and
                    mc.member_id = m.member_id and mc.contact_type_id = 2
                    and ca.contact_id = mc.contact_id order by matrik1_level, m.member_id
                ";


            $stmt = $this->_db->query($sql);
            $res = $stmt->execute(array($key));    

            $this->tmpTree = "";

            $cnt = 0;



            $this->tmpTree .= "<ul>\n";

            //for($k = 0;$k<$num;$k++) {

            while($res->next()){
                $result = $res->current();
                $cnt++;
                $this->tmpTree .= "[SPONSOR1]";
                $sponsor[] = "<li title='<center>".strtoupper($result['member_name']."<br>".$result['mobile_no']."<br>".$result['addr'])."</center>' data-actor='".$result['member_id']."' class='small' ><br>".$result['member_code'];


                $sql2 = "select m.*, ca.contact_mobile, concat(ca.contact_address,' ',ca.contact_city) as addr
                        from members m, member_contact mc, contact ca
                        where m.$sBy  = ? and (member_type_id = 2 or member_type_id > 4) and
                        mc.member_id = m.member_id and mc.contact_type_id = 2
                        and ca.contact_id = mc.contact_id order by matrik1_level, m.member_id
                        ";

                $stmt2 = $this->_db->query($sql2);
                $res2 = $stmt2->execute(array($result['sponsor_id']));

                $this->tmpTree .= "<ul>\n";

                while($res2->next()){
                    $result2 = $res2->current();
                    
                    $cnt++;
                    $this->tmpTree .= "[SPONSOR2]";
                    $sponsor[] = "<li title='<center>".strtoupper($result2['member_name']."<br>".$result2['mobile_no']."<br>".$result2['addr']."</center>' data-actor='".$result2['member_id'])."' class='small'><br>".$result2['member_code'];


                    $sql3 = "select m.*, ca.contact_mobile, concat(ca.contact_address,' ',ca.contact_city) as addr
                            from members m, member_contact mc, contact ca
                            where m.$sBy  = ? and (member_type_id = 2 or member_type_id > 4) and
                            mc.member_id = m.member_id and mc.contact_type_id = 2
                            and ca.contact_id = mc.contact_id order by matrik1_level, m.member_id
                        ";

                    $stmt3 = $this->_db->query($sql3);
                    $res3 = $stmt3->execute(array($result2['sponsor_id']));                   

                   
                    $this->tmpTree .= "<ul>\n";

                    while($res3->next()){
                        $result3 = $res3->current();
                        $cnt++;
                        $this->tmpTree .= "[SPONSOR3]";
                        $sponsor[] = "<li title='<center>".strtoupper($result3['member_name']."<br>".$result3['mobile_no']."<br>".$result3['addr'])."</center>' data-actor='".$result3['member_id']."' class='small'><br>".$result3['member_code'];


                        $sql4 = "select m.*, ca.contact_mobile, concat(ca.contact_address,' ',ca.contact_city) as addr
                                from members m, member_contact mc, contact ca
                                where m.$sBy  = ? and (member_type_id = 2 or member_type_id > 4) and
                                mc.member_id = m.member_id and mc.contact_type_id = 2
                                and ca.contact_id = mc.contact_id order by matrik1_level, m.member_id
                            ";

                        $stmt4 = $this->_db->query($sql4);
                        $res4 = $stmt4->execute(array($result3['sponsor_id']));

                        
                        $this->tmpTree .= "<ul>\n";

                        while($res4->next()){
                            $result4 = $res4->current();
                            $cnt++;
                            $this->tmpTree .= "[SPONSOR4]";
                            $sponsor[] = "<li title='<center>".strtoupper($result4['member_name']."<br>".$result4['mobile_no']."<br>".$result4['addr'])."</center>' data-actor='".$result4['member_id']."' class='small'><br>".$result4['member_code'];


                            $sql5 = "select m.*, ca.contact_mobile, concat(ca.contact_address,' ',ca.contact_city) as addr
                                    from members m, member_contact mc, contact ca
                                    where m.$sBy  = ? and (member_type_id = 2 or member_type_id > 4) and
                                    mc.member_id = m.member_id and mc.contact_type_id = 2
                                    and ca.contact_id = mc.contact_id order by matrik1_level, m.member_id
                                ";

                            $stmt5 = $this->_db->query($sql5);
                            $res5 = $stmt5->execute(array($result4['sponsor_id']));                           

                         
                            $this->tmpTree .= "<ul>\n";

                            while($res5->next()){
                                $result5 = $res5->current();
                                $this->tmpTree .= "[SPONSOR5]";
                                $sponsor[] = "<li title='<center>".strtoupper($result5['member_name']."<br>".$result5['mobile_no']."<br>".$result5['addr'])."</center>' data-actor='".$result5['member_id']."' class='small'><br>".$result5['member_code'];
                            }

                            $this->tmpTree .= "</ul>\n";
                            
                        }

                        $this->tmpTree .= "</ul>\n";

                    }

                    $this->tmpTree .= "</ul>\n";
                    

                }

                $this->tmpTree .= "</ul>\n";
                    
            }

            $this->tmpTree .= "</ul>\n";

            if ($cnt > 0){
                krsort($sponsor);
                $z = 1;
                foreach ($sponsor as $key => $val) {
                    $this->tmpTree = str_replace('[SPONSOR'.$z.']', $val, $this->tmpTree);
                    $z++;
                }
            }

            $resTree = array("tree" => $this->tmpTree);

            return $resTree;

    }


    function getMemberNetwork($sBy, $key, $parentCode) {
            /*
            $sql = "SELECT  level, hi.*, mc.mobile_no, concat(ca.addr3,' ',ca.addr4) as addr
                    FROM (
                    SELECT  CPriorMatrik(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $key, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    JOIN member_contact mc
                    ON hi.member_id = mc.member_id
                    JOIN contact_address ca
                    ON mc.member_contact_id = ca.member_contact_id
                    WHERE mc.contact_type_id = 1 and level = 1";
             *
             */

            $sql = "SELECT  level, hi.*
                    FROM (
                    SELECT  CPriorMatrik(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := ?, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    and level = 1 order by pos";

            $stmt = $this->_db->query($sql);
            $result = $stmt->execute(array($key));

            return $result;
    }


    function getMemberTrinary($sBy, $key, $parentCode) {
          
            $sql = "SELECT  level, hi.*
                    FROM (
                    SELECT  CPriorTrinary(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := ?, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    and level = 1";

            $stmt = $this->_db->query($sql);
            $result = $stmt->execute(array($key));

            return $result;
    }


    function getSponsor($memberId){
        
        $sql = "SELECT m.member_id as id, parent_id, sponsor_id, start_dtm, member_code, member_name, img_profile,
                concat(ma.contact_address, ' ', ma.contact_city, ' ', ma.contact_province) as address,
                contact_phone, contact_email, contact_mobile,
                CASE date_format(start_dtm, '%d%m%Y')
                  WHEN date_format(now(),'%d%m%Y') THEN 'Today'
                  WHEN date_format(DATE_ADD(now(),INTERVAL -1 DAY),'%d%m%Y') THEN 'Yesterday'
                ELSE date_format(start_dtm, '%d %b %Y') END
                as dateline,
                date_format(start_dtm, '%H:%i') timeline
                from members m, member_contact mc, contact ma
                where sponsor_id = $memberId and mc.member_id = m.member_id and ma.contact_id = mc.contact_id
                order by m.member_id desc, sponsor_id  ";
                    //limit $start, $limit";


        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();

        return $result;
    }


    function getSponsorx($memberId, $when){

        $sql = "select * from (SELECT m.member_id as id, m.sponsor_id as _parentId, member_code, member_name,
                concat(ma.contact_address, ' ', ma.contact_city, ' ', ma.contact_province) as address,
                'closed' as state, img_profile,
                CASE date_format(start_dtm, '%d%m%Y')
                  WHEN date_format(now(),'%d%m%Y') THEN 'Today'
                  WHEN date_format(DATE_ADD(now(),INTERVAL -1 DAY),'%d%m%Y') THEN 'Yesterday'
                ELSE date_format(start_dtm, '%d %b %Y') END
                as dateline, date_format(start_dtm, '%H:%i') timeline
                FROM (
                    SELECT member_id, parent_id, 0 as sponsor_id,
                      start_dtm, member_code, member_name, img_profile
                    from members where member_id = $memberId
                    union
                    SELECT hi.member_id, parent_id, hi.sponsor_id,
                        hi.start_dtm, hi.member_code, hi.member_name, hi.img_profile
                    FROM (
                        SELECT  CPrior(member_id) AS member_id, @level AS level
                        FROM (
                            SELECT  @start_with := $memberId,@id := @start_with,@level := 0
                            ) vars, members
                        WHERE   @id IS NOT NULL
                        ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id) m, member_contact mc, contact ma
                    where  mc.member_id = m.member_id and ma.contact_id = mc.contact_id
                    order by id desc, sponsor_id desc) xx ";
                    //limit $start, $limit";

        if ($when != "") $sql .= " where dateline = '$when'";
        //else $sql .= " ";

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();

        return $result;
    }


    public function getJumlahPasangan($memberId){
        /*
        $sql = "select concat(FORMAT(COALESCE(sum_ki,0),0),' | ',FORMAT(COALESCE(sum_ka,0),0)) as jml
                from t_bonus_pasangan where member_id = $memberId and period = DATE_FORMAT(now(), '%Y%m%d')";

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();
        $jml = 0;
        while($result->next()){
            $res = $result->current();
            $jml = $res['jml'];
        }
        return $jml;
         *
         */
        return;
    }


    function getMemberBinary($sBy, $key, $parentCode) {
            
           $sql = "select *
                    from members
                    where $sBy  = ?
                    order by pos";

            $stmt = $this->_db->query($sql);
            $result = $stmt->execute(array($key));
            $num = 0;
            while($result->next()){
                //$res = $result->current();
                $num++;
            }


            if ($num > 0){
            
                $this->tmpTree .= "<ul>\n";
                $result = $stmt->execute(array($key));
                while($result->next()){
                    $res = $result->current();
                    
                    $imgPic = ($res['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                    $detailStts = "";
                    if ($res['status_id'] == 2){

                        $sqlP = "select member_type_name,
                                (select member_type_name
                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                from member_type mt where member_type_id = ?";

                        $stmtP = $this->_db->query($sqlP);
                        $resultP = $stmtP->execute(array($res['member_type_id']));
                        while($resultP->next()){
                            $resP = $resultP->current();
                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res['subs_amount'], 2, '.', ',');
                        }
                    }

                    $stts = ($res['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                    if ($num == 1){
                        if ($res['pos'] == 1){                            

                            $this->tmpTree .= "<li data-actor='child-".$res['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res['member_id']) ." <br>\n";
                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res['member_code']." <br> ".$res['member_name']."\n";

                            $sql2 = "select *
                                    from members
                                    where $sBy  = ?
                                    order by pos";


                            $stmt2 = $this->_db->query($sql2);
                            $result2 = $stmt2->execute(array($res['member_id']));
                            $num2 = 0;
                            while($result2->next()){
                                
                                $num2++;
                            }

                            if ($num2 > 0){
                                $this->tmpTree .= "<ul>\n";

                                $result2 = $stmt2->execute(array($res['member_id']));
                                while($result2->next()){
                                    $res2 = $result2->current();
                                    
                                    $imgPic = ($res2['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                    $detailStts = "";
                                    if ($res2['status_id'] == 2){
                                        
                                        $sqlP = "select member_type_name,
                                                (select member_type_name
                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                from member_type mt where member_type_id = ?";

                                        $stmtP = $this->_db->query($sqlP);
                                        $resultP = $stmtP->execute(array($res2['member_type_id']));
                                        while($resultP->next()){
                                            $resP = $resultP->current();
                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res2['subs_amount'], 2, '.', ',');
                                        }
                                    }

                                    $stts = ($res2['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                    if ($num2 == 1){
                                        if ($res2['pos'] == 1){
                                            
                                            $this->tmpTree .= "<li data-actor='child-".$res2['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res2['member_id']) ." <br>\n";
                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res2['member_code']." <br> ".$res2['member_name']."\n";

                                            $sql4 = "select *
                                                    from members
                                                    where $sBy  = ?
                                                    order by pos";


                                            $stmt4 = $this->_db->query($sql4);
                                            $result4 = $stmt4->execute(array($res2['member_id']));
                                            $num4 = 0;
                                            while($result4->next()){

                                                $num4++;
                                            }

                                            if ($num4 > 0){
                                                $this->tmpTree .= "<ul>\n";

                                                $result4 = $stmt4->execute(array($res2['member_id']));
                                                while($result4->next()){
                                                    $res4 = $result4->current();

                                                    $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                    $detailStts = "";
                                                    if ($res4['status_id'] == 2){

                                                        $sqlP = "select member_type_name,
                                                                (select member_type_name
                                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                                from member_type mt where member_type_id = ?";

                                                        $stmtP = $this->_db->query($sqlP);
                                                        $resultP = $stmtP->execute(array($res4['member_type_id']));
                                                        while($resultP->next()){
                                                            $resP = $resultP->current();
                                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                                        }
                                                    }

                                                    $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                                    if ($num4 == 1){
                                                        if ($res4['pos'] == 1){
                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";


                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a>\n";

                                                        }else{
                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a>\n";

                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                                        }

                                                    }else{
                                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";
                                                    }
                                                }

                                                $this->tmpTree .= "</ul>\n";
                                            }else{

                                                $this->tmpTree .= "<ul>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a></li>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a></li>\n";

                                                $this->tmpTree .= "</ul>\n";

                                                $this->tmpTree .= "</li>\n";

                                            }


                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a>\n";

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";
                                            

                                        }else{
                                            
                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a>\n";

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";

                                            

                                            $this->tmpTree .= "<li data-actor='child-".$res2['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res2['member_id']) ." <br>\n";
                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res2['member_code']." <br> ".$res2['member_name']."\n";

                                            $sql4 = "select *
                                                    from members
                                                    where $sBy  = ?
                                                    order by pos";


                                            $stmt4 = $this->_db->query($sql4);
                                            $result4 = $stmt4->execute(array($res2['member_id']));
                                            $num4 = 0;
                                            while($result4->next()){

                                                $num4++;
                                            }

                                            if ($num4 > 0){
                                                $this->tmpTree .= "<ul>\n";

                                                $result4 = $stmt4->execute(array($res2['member_id']));
                                                while($result4->next()){
                                                    $res4 = $result4->current();

                                                    $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";
                                                    
                                                    $detailStts = "";
                                                    if ($res4['status_id'] == 2){

                                                        $sqlP = "select member_type_name,
                                                                (select member_type_name
                                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                                from member_type mt where member_type_id = ?";

                                                        $stmtP = $this->_db->query($sqlP);
                                                        $resultP = $stmtP->execute(array($res4['member_type_id']));
                                                        while($resultP->next()){
                                                            $resP = $resultP->current();
                                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                                        }
                                                    }

                                                    $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                                    if ($num4 == 1){
                                                        if ($res4['pos'] == 1){
                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";


                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a>\n";


                                                        }else{
                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a>\n";

                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                                        }

                                                    }else{
                                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";
                                                    }
                                                }

                                                $this->tmpTree .= "</ul>\n";
                                            }else{

                                                $this->tmpTree .= "<ul>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a></li>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a></li>\n";

                                                $this->tmpTree .= "</ul>\n";

                                                $this->tmpTree .= "</li>\n";

                                            }

                                        }
                                        
                                    }else{
                                        $this->tmpTree .= "<li data-actor='child-".$res2['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res2['member_id']) ." <br> \n";
                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res2['member_code']." <br> ".$res2['member_name']."\n";

                                        $sql4 = "select *
                                                    from members
                                                    where $sBy  = ?
                                                    order by pos";


                                        $stmt4 = $this->_db->query($sql4);
                                        $result4 = $stmt4->execute(array($res2['member_id']));
                                        $num4 = 0;
                                        while($result4->next()){

                                            $num4++;
                                        }

                                        if ($num4 > 0){
                                            $this->tmpTree .= "<ul>\n";

                                            $result4 = $stmt4->execute(array($res2['member_id']));
                                            while($result4->next()){
                                                $res4 = $result4->current();

                                                $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                $detailStts = "";
                                                if ($res4['status_id'] == 2){

                                                    $sqlP = "select member_type_name,
                                                            (select member_type_name
                                                            from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                            from member_type mt where member_type_id = ?";

                                                    $stmtP = $this->_db->query($sqlP);
                                                    $resultP = $stmtP->execute(array($res4['member_type_id']));
                                                    while($resultP->next()){
                                                        $resP = $resultP->current();
                                                        $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                                    }
                                                }

                                                $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                                if ($num4 == 1){
                                                    if ($res4['pos'] == 1){
                                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";


                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a>\n";


                                                    }else{
                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a>\n";

                                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                                    }

                                                }else{
                                                    $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";
                                                }
                                            }

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";
                                            
                                        }else{

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=1'>Empty</a></li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res2['member_id']."&pos=2'>Empty</a></li>\n";

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";

                                        }

                                    }
                                }

                                $this->tmpTree .= "</ul>\n";
                            }else{
                                
                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=1'>Empty</a>\n";

                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n"; 

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a>\n";

                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";

                            }
                            
                            
                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$key."&pos=2'>Empty</a>\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";


                        }else{
                            
                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$key."&pos=1'>Empty</a>\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "</ul>\n";


                            $this->tmpTree .= "<li data-actor='child-".$res['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res['member_id']) ." <br> \n";
                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res['member_code']." <br> ".$res['member_name']."\n";

                            
                            $sql3 = "select *
                                    from members
                                    where $sBy  = ?
                                    order by pos";


                            $stmt3 = $this->_db->query($sql3);
                            $result3 = $stmt3->execute(array($res['member_id']));
                            $num3 = 0;
                            while($result3->next()){
                                $res3 = $result3->current();
                                $num3++;
                            }


                            if ($num3 > 0){
                                $this->tmpTree .= "<ul>\n";
                                
                                $result3 = $stmt3->execute(array($res['member_id']));
                                while($result3->next()){
                                    $res3 = $result3->current();

                                    $imgPic = ($res3['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                    $detailStts = "";
                                    if ($res3['status_id'] == 2){

                                        $sqlP = "select member_type_name,
                                                (select member_type_name
                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                from member_type mt where member_type_id = ?";

                                        $stmtP = $this->_db->query($sqlP);
                                        $resultP = $stmtP->execute(array($res3['member_type_id']));
                                        while($resultP->next()){
                                            $resP = $resultP->current();
                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res3['subs_amount'], 2, '.', ',');
                                        }
                                    }

                                    $stts = ($res3['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";
                                    
                                    if ($num3 == 1){
                                        if ($res3['pos'] == 1){

                                            $this->tmpTree .= "<li data-actor='child-".$res3['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res3['member_id']) ." <br> \n";
                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res3['member_code']." <br> ".$res3['member_name']."\n";



                                            $sql4 = "select *
                                                    from members
                                                    where $sBy  = ?
                                                    order by pos";


                                            $stmt4 = $this->_db->query($sql4);
                                            $result4 = $stmt4->execute(array($res3['member_id']));
                                            $num4 = 0;
                                            while($result4->next()){
                                                $res4 = $result4->current();
                                                $num4++;
                                            }


                                            if ($num4 > 0){
                                                $this->tmpTree .= "<ul>\n";

                                                $result4 = $stmt4->execute(array($res3['member_id']));
                                                while($result4->next()){
                                                    $res4 = $result4->current();

                                                    $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                    $detailStts = "";
                                                    if ($res4['status_id'] == 2){

                                                        $sqlP = "select member_type_name,
                                                                (select member_type_name
                                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                                from member_type mt where member_type_id = ?";

                                                        $stmtP = $this->_db->query($sqlP);
                                                        $resultP = $stmtP->execute(array($res4['member_type_id']));
                                                        while($resultP->next()){
                                                            $resP = $resultP->current();
                                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                                        }
                                                    }

                                                    $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";
                                                    

                                                    if ($num4 == 1){
                                                        if ($res4['pos'] == 1){
                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";

                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=2'>Empty</a></li>\n";

                                                        }else{
                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=1'>Empty</a></li>\n";

                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                                        }
                                                    }
                                                }

                                                $this->tmpTree .= "</ul>\n";

                                            }else{

                                                $this->tmpTree .= "<ul>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=1'>Empty</a></li>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=2'>Empty</a></li>\n";

                                                $this->tmpTree .= "</ul>\n";

                                                $this->tmpTree .= "</li>\n";
                                            }

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a>\n";

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "</ul></li>\n";
                                            
                                        }else{
                                            
                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=1'>Empty</a>\n";

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                            $this->tmpTree .= "</ul></li>\n";

                                            $this->tmpTree .= "<li data-actor='child-".$res3['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res3['member_id']) ." <br> \n";
                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res3['member_code']." <br> ".$res3['member_name']."\n";



                                            $sql4 = "select *
                                                    from members
                                                    where $sBy  = ?
                                                    order by pos";


                                            $stmt4 = $this->_db->query($sql4);
                                            $result4 = $stmt4->execute(array($res3['member_id']));
                                            $num4 = 0;
                                            while($result4->next()){
                                                $res4 = $result4->current();
                                                $num4++;
                                            }


                                            if ($num4 > 0){
                                                $this->tmpTree .= "<ul>\n";

                                                $result4 = $stmt4->execute(array($res3['member_id']));
                                                while($result4->next()){
                                                    $res4 = $result4->current();

                                                    $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                    $detailStts = "";
                                                    if ($res4['status_id'] == 2){

                                                        $sqlP = "select member_type_name,
                                                                (select member_type_name
                                                                from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                                from member_type mt where member_type_id = ?";

                                                        $stmtP = $this->_db->query($sqlP);
                                                        $resultP = $stmtP->execute(array($res4['member_type_id']));
                                                        while($resultP->next()){
                                                            $resP = $resultP->current();
                                                            $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                                        }
                                                    }

                                                    $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                                    if ($num4 == 1){
                                                        if ($res4['pos'] == 1){
                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br>\n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."</li>\n";

                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=2'>Empty</a></li>\n";

                                                        }else{
                                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res3['member_id']."&pos=1'>Empty</a></li>\n";

                                                            $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                                            $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                                        }
                                                    }
                                                }

                                                $this->tmpTree .= "</ul>\n";

                                            }else{

                                                $this->tmpTree .= "<ul>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a></li>\n";

                                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";

                                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                                $this->tmpTree .= "</ul>\n";

                                                $this->tmpTree .= "</li>\n";
                                            }
                                        }
                                    }
                                }

                                $this->tmpTree .= "</ul>\n";
                                
                            }else{

                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=1'>Empty</a>\n";

                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";

                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a>\n";

                                $this->tmpTree .= "<ul>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";

                                $this->tmpTree .= "</ul>\n";

                                $this->tmpTree .= "</li>\n";
                            }

                        }

                    }else{

                        $this->tmpTree .= "<li data-actor='child-".$res['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res['member_id']) ." <br> \n";
                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res['member_code']." <br> ".$res['member_name']."\n";

                        $sql4 = "select *
                                from members
                                where $sBy  = ?
                                order by pos";


                        $stmt4 = $this->_db->query($sql4);
                        $result4 = $stmt4->execute(array($res['member_id']));
                        $num4 = 0;
                        while($result4->next()){
                            $res4 = $result4->current();
                            $num4++;
                        }

                        if ($num4 > 0){
                            $this->tmpTree .= "<ul>\n";

                            $result4 = $stmt4->execute(array($res['member_id']));
                            while($result4->next()){
                                $res4 = $result4->current();
                                
                                $imgPic = ($res4['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                $detailStts = "";
                                if ($res4['status_id'] == 2){

                                    $sqlP = "select member_type_name,
                                            (select member_type_name
                                            from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                            from member_type mt where member_type_id = ?";

                                    $stmtP = $this->_db->query($sqlP);
                                    $resultP = $stmtP->execute(array($res4['member_type_id']));
                                    while($resultP->next()){
                                        $resP = $resultP->current();
                                        $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res4['subs_amount'], 2, '.', ',');
                                    }
                                }

                                $stts = ($res4['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";
                                
                                if ($num4 == 1){
                                    if ($res4['pos'] == 1){
                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                        $sql5 = "select *
                                                from members
                                                where $sBy  = ?
                                                order by pos";


                                        $stmt5 = $this->_db->query($sql5);
                                        $result5 = $stmt5->execute(array($res4['member_id']));
                                        $num5 = 0;
                                        while($result5->next()){
                                            $res5 = $result5->current();
                                            $num5++;
                                        }

                                        if ($num5 > 0){
                                            $this->tmpTree .= "<ul>\n";

                                            $result5 = $stmt5->execute(array($res4['member_id']));
                                            while($result5->next()){
                                                $res5 = $result5->current();

                                                $imgPic = ($res5['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                $detailStts = "";
                                                if ($res5['status_id'] == 2){

                                                    $sqlP = "select member_type_name,
                                                            (select member_type_name
                                                            from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                            from member_type mt where member_type_id = ?";

                                                    $stmtP = $this->_db->query($sqlP);
                                                    $resultP = $stmtP->execute(array($res5['member_type_id']));
                                                    while($resultP->next()){
                                                        $resP = $resultP->current();
                                                        $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res5['subs_amount'], 2, '.', ',');
                                                    }
                                                }

                                                $stts = ($res5['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";
                                                
                                                if ($num5 == 1){
                                                    if ($res5['pos'] == 1){
                                                        
                                                        $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";

                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                                    }else{
                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                                        $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."\n";
                                                    }
                                                }else{
                                                    $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";
                                                }
                                            }

                                            $this->tmpTree .= "</ul>\n";

                                        }else{

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";
                                        }

                                        $this->tmpTree .= "</li>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a>\n";

                                        $this->tmpTree .= "<ul>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                        $this->tmpTree .= "</ul>\n";
                                        $this->tmpTree .= "</li>\n";

                                    }else{
                                        
                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=1'>Empty</a>\n";
                                        $this->tmpTree .= "<ul>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                                        $this->tmpTree .= "</ul>\n";
                                        $this->tmpTree .= "</li>\n";

                                        $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                        $sql5 = "select *
                                                from members
                                                where $sBy  = ?
                                                order by pos";


                                        $stmt5 = $this->_db->query($sql5);
                                        $result5 = $stmt5->execute(array($res4['member_id']));
                                        $num5 = 0;
                                        while($result5->next()){
                                            $res5 = $result5->current();
                                            $num5++;
                                        }

                                        if ($num5 > 0){
                                            $this->tmpTree .= "<ul>\n";

                                            $result5 = $stmt5->execute(array($res4['member_id']));
                                            while($result5->next()){
                                                $res5 = $result5->current();

                                                $imgPic = ($res5['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                                $detailStts = "";
                                                if ($res5['status_id'] == 2){

                                                    $sqlP = "select member_type_name,
                                                            (select member_type_name
                                                            from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                            from member_type mt where member_type_id = ?";

                                                    $stmtP = $this->_db->query($sqlP);
                                                    $resultP = $stmtP->execute(array($res5['member_type_id']));
                                                    while($resultP->next()){
                                                        $resP = $resultP->current();
                                                        $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res5['subs_amount'], 2, '.', ',');
                                                    }
                                                }

                                                $stts = ($res5['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                                if ($num5 == 1){
                                                    if ($res5['pos'] == 1){
                                                        $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";

                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                                    }else{
                                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                                        $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                        $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."\n";
                                                    }
                                                }else{
                                                    $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";
                                                }
                                            }

                                            $this->tmpTree .= "</ul>\n";

                                        }else{

                                            $this->tmpTree .= "<ul>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                            $this->tmpTree .= "</ul>\n";

                                            $this->tmpTree .= "</li>\n";
                                        }
                                    }
                                }else{
                                    $this->tmpTree .= "<li data-actor='child-".$res4['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res4['member_id']) ." <br> \n";
                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res4['member_code']." <br> ".$res4['member_name']."\n";

                                    $sql5 = "select *
                                            from members
                                            where $sBy  = ?
                                            order by pos";


                                    $stmt5 = $this->_db->query($sql5);
                                    $result5 = $stmt5->execute(array($res4['member_id']));
                                    $num5 = 0;
                                    while($result5->next()){
                                        $res5 = $result5->current();
                                        $num5++;
                                    }

                                    if ($num5 > 0){
                                        $this->tmpTree .= "<ul>\n";

                                        $result5 = $stmt5->execute(array($res4['member_id']));
                                        while($result5->next()){
                                            $res5 = $result5->current();

                                            $imgPic = ($res5['status_id'] == 2) ? "pic-member-active.png" : "pic-member-inactive.png";

                                            $detailStts = "";
                                            if ($res5['status_id'] == 2){

                                                $sqlP = "select member_type_name,
                                                        (select member_type_name
                                                        from member_type where member_type_id = mt.parent_member_type_id) as pkg
                                                        from member_type mt where member_type_id = ?";

                                                $stmtP = $this->_db->query($sqlP);
                                                $resultP = $stmtP->execute(array($res5['member_type_id']));
                                                while($resultP->next()){
                                                    $resP = $resultP->current();
                                                    $detailStts = "Package: ".$resP["pkg"]."<br>Plan: ".$resP["member_type_name"]."<br>Amount: ".number_format($res5['subs_amount'], 2, '.', ',');
                                                }
                                            }

                                            $stts = ($res5['status_id'] == 2) ? "<center>Active<br>".$detailStts."<center>" : "Inactive";

                                            if ($num5 == 1){
                                                if ($res5['pos'] == 1){
                                                    $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";

                                                    $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                    $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                                }else{
                                                    $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                                    $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                                    $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                    $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."\n";
                                                }
                                            }else{
                                                $this->tmpTree .= "<li data-actor='child-".$res5['member_id']."' class='big' title='".$stts."'>". $this->getJumlahPasangan($res5['member_id']) ." <br> \n";
                                                $this->tmpTree .= "<img src='/img/".$imgPic."'/><br><br>".$res5['member_code']." <br> ".$res5['member_name']."</li>\n";
                                            }
                                        }

                                        $this->tmpTree .= "</ul>\n";

                                    }else{

                                        $this->tmpTree .= "<ul>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=1'>Empty</a></li>\n";

                                        $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                                        $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res4['member_id']."&pos=2'>Empty</a></li>\n";

                                        $this->tmpTree .= "</ul>\n";

                                        $this->tmpTree .= "</li>\n";
                                    }

                                    $this->tmpTree .= "</li>\n";
                                }
                            }

                            $this->tmpTree .= "</ul>\n";
                            
                        }else{

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=1'>Empty</a>\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$res['member_id']."&pos=2'>Empty</a>\n";

                            $this->tmpTree .= "<ul>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                            $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";
                            
                            $this->tmpTree .= "</ul>\n";

                            $this->tmpTree .= "</li>\n";
                        }


                    }
                }

                $this->tmpTree .= "</ul>\n";

            }else{

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$key."&pos=1'>Empty</a>\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty. Click to add Team'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br><a href='../member/addmember?upid=".$key."&pos=2'>Empty</a>\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";

                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty\n";


                $this->tmpTree .= "<ul>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "<li class='big' title='Empty'><br>\n";
                $this->tmpTree .= "<img src='/img/pic-member-empty.png'/><br><br>Empty</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";

                $this->tmpTree .= "</ul>\n";

                $this->tmpTree .= "</li>\n";
            }



            //$resTree = array("tree" => $this->tmpTree);

            return $this->tmpTree;

    }



    

    public function getDownlineTeam__($type, $memberId, $start, $limit, $searchField = "") {

        // $type ==> 1=direct referral; 2=all; 3=left; 4=right

        
        if ($type == 3 || $type == 4){
            $sql = "SELECT
                        MAX(IF(pos = 1, member_id, NULL)) as member_left,
                        MAX(IF(pos = 2, member_id, NULL)) as member_right
                  FROM members where parent_id = ?";

            $stmt = $this->_db->query($sql);
            $getPosId = $stmt->execute(array($memberId));

            while($getPosId->next()){
                    $pos = $getPosId->current();
                    $leftId = ($pos['member_left'] != "") ? $pos['member_left'] : 0;
                    $rightId = ($pos['member_right'] != "") ? $pos['member_right'] : 0;

            }
        }



        if ($type == 1){

            $sql = "select count(1) as total
                    from members
                    where sponsor_id = $memberId ";
            
        }else if ($type == 2){

            $sql = "select count(1) as total from
                    (SELECT  level, hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $memberId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";

        }else if ($type == 3){

            $sql = "select count(1) as total from
                    (
                    select * from members where member_id = $leftId
                    union
                    SELECT  hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $leftId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";

        }else if ($type == 4){

            $sql = "select count(1) as total from
                    (
                    select * from members where member_id = $rightId
                    union
                    SELECT  hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $rightId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";
            
        }
        
        if ($searchField != ""){
            if ($type == 2){
                $sql .= " where ";
            }else $sql .= " and ";

            $sql .= " $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];

        }


        $data = array($rows);

        $dataDL = array();
        
        if ($type == 1){

            $sql = "select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    getCountChildPos(member_id, 1) as lf, getCountChildPos(member_id, 2) as rg
                    from members
                    where sponsor_id = $memberId ";
            
        }else if ($type == 2){

            $sql = "SELECT  DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    getCountChildPos(hi.member_id, 1) as lf, getCountChildPos(hi.member_id, 2) as rg
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $memberId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ";

        }else if ($type == 3){

            $sql = "select * from (
                    SELECT  DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    getCountChildPos(hi.member_id, 1) as lf, getCountChildPos(hi.member_id, 2) as rg
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $leftId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    getCountChildPos(member_id, 1) as lf, getCountChildPos(member_id, 2) as rg
                    from members where member_id = $leftId
                    ) x order by start_dtm
                    ";

        }else if ($type == 4){

            $sql = "select * from (
                    SELECT  DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    getCountChildPos(hi.member_id, 1) as lf, getCountChildPos(hi.member_id, 2) as rg
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $rightId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    getCountChildPos(member_id, 1) as lf, getCountChildPos(member_id, 2) as rg
                    from members where member_id = $rightId
                    ) x order by start_dtm 
                    ";

        }

        if ($searchField != ""){
            if ($type == 2){
                $sql .= " where ";
            }else $sql .= " and ";

            $sql .= " $searchField ";
        }

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();

        
        while($result->next()){
                $res = $result->current();

                $total = $res['lf']+$res['rg'];

                array_push($dataDL, array("joindt" => $res['start_dtm'], "idno" => $res['member_code'], "name" => $res['member_name'],
                                            "left" => $res['lf'], "right" => $res['rg'], "total" => $total));
        }


        array_push($data, $dataDL);

        return $data;
    }


    public function getDownlineTeam($type, $memberId, $start, $limit, $searchField = "") {

        // $type ==> 1=direct referral; 2=all; 3=left; 4=right


        if ($type > 1){
            $sql = "SELECT
                        MAX(IF(pos = 1, member_id, NULL)) as member_left,
                        MAX(IF(pos = 2, member_id, NULL)) as member_right
                  FROM members where parent_id = ?";


            $stmt = $this->_db->query($sql);
            $getPosId = $stmt->execute(array($memberId));

            while($getPosId->next()){
                    $pos = $getPosId->current();
                    $leftId = ($pos['member_left'] != "") ? $pos['member_left'] : 0;
                    $rightId = ($pos['member_right'] != "") ? $pos['member_right'] : 0;

            }
        }



        if ($type == 1){

            $sql = "select count(1) as total
                    from members
                    where sponsor_id = $memberId ";

        }else if ($type == 2){

            $sql = "select count(1) as total from
                    (SELECT  level, hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $memberId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";

        }else if ($type == 3){

            $sql = "select count(1) as total from
                    (
                    select * from members where member_id = $leftId
                    union
                    SELECT  hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $leftId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";

        }else if ($type == 4){

            $sql = "select count(1) as total from
                    (
                    select * from members where member_id = $rightId
                    union
                    SELECT  hi.*
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $rightId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    ) xx ";

        }

        if ($searchField != ""){
            if ($type == 2){
                $sql .= " where ";
            }else $sql .= " and ";

            $sql .= " $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute();

        while($getTotal->next()){
                $res = $getTotal->current();
                $rows = $res['total'];

        }


        $data = array($rows);

        $dataDL = array();

        if ($type == 1){

            $sql = "select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    CASE pos WHEN 1 THEN concat(subs_amount,'|',0)
                    WHEN 2 THEN concat(0,'|',subs_amount) END
                    as amt_pos
                    from members
                    where sponsor_id = $memberId order by start_dtm desc ";

            /*
            $sql = "select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    getCountChildPos(member_id, 1) as lf, getCountChildPos(member_id, 2) as rg
                    from members
                    where sponsor_id = $memberId ";
             * 
             */


        }else if ($type == 2){

           
            $sql = "SELECT  DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    getAmountPos($memberId, hi.member_id, hi.subs_amount) as amt_pos
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $memberId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    order by hi.start_dtm desc
                    ";
           

                    /*
             $sql = "select * from (
                    select * from (
                    SELECT DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    hi.subs_amount as lf, 0 as rg
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $leftId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    subs_amount as lf, 0 as rg
                    from members where member_id = $leftId
                    ) x 
                    ";

              $sql .= " union 
                    select * from (
                    SELECT DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    0 as lf, hi.subs_amount as rg
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $rightId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    0 as lf, subs_amount as rg
                    from members where member_id = $rightId
                    ) x
                    ) y order by start_dtm
                    ";
                     * 
                     */


        }else if ($type == 3){

            $sql = "select * from (
                    SELECT  hi.start_dtm as st, DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    concat(hi.subs_amount, '|', 0) as amt_pos
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $leftId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select start_dtm as st, DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    concat(subs_amount, '|', 0) as amt_pos
                    from members where member_id = $leftId
                    ) x order by st desc
                    ";

        }else if ($type == 4){

            $sql = "select * from (
                    SELECT hi.start_dtm as st, DATE_FORMAT(hi.start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, hi.member_code, hi.member_name,
                    concat(0, '|', hi.subs_amount) as amt_pos
                    FROM (
                    SELECT  CPriorChild(member_id) AS member_id, @level AS level
                    FROM (
                    SELECT  @start_with := $rightId, @id := @start_with,@level := 0
                    ) vars, members
                    WHERE   @id IS NOT NULL
                    ) ho
                    JOIN    members hi
                    ON      hi.member_id = ho.member_id
                    union
                    select start_dtm as st, DATE_FORMAT(start_dtm,'%d-%m-%Y %H:%i:%s') as start_dtm, member_code, member_name,
                    concat(0, '|', subs_amount) as amt_pos
                    from members where member_id = $rightId
                    ) x order by st desc
                    ";

        }

        if ($searchField != ""){
            if ($type == 2){
                $sql .= " where ";
            }else $sql .= " and ";

            $sql .= " $searchField ";
        }

        if ($limit != ""){
            $sql .= " limit $start, $limit";
        }

        $stmt = $this->_db->query($sql);
        $result = $stmt->execute();


        while($result->next()){
                $res = $result->current();

                $amtPos = explode("|", $res["amt_pos"]);

                $lf = $amtPos[0];
                $rg = $amtPos[1];

                $total = $lf+$rg;

                array_push($dataDL, array("joindt" => $res['start_dtm'], 
                                          "idno" => $res['member_code'],
                                          "name" => $res['member_name'],
                                          "left" => $lf,
                                          "right" => $rg,
                                          "total" => $total));
        }


        array_push($data, $dataDL);

        return $data;
    }

}
